Visitor counter

2021-6-20

I made a visitor counter in Python, which works by scanning the nginx
access.log
and extracting the time, URL, bytes transferred and HTTP status code of every entry.
It makes a dictionary of unique combinations of URLs and HTTP status codes as keys and the number of times they occur as values.
This dictionary, the latest time and the summed byte count (not site-specific) is saved in a JSON file.
This file is opened on the next start of the program, the log read and then each entry in the log only after the stored time is processed to avoid duplicates.
Currently nothing further is planned with the JSON but maybe a counter on each page will be implemented soon.
The script is called in the
prerotate
block of the logrotate file responsible for nginx.
I made a visitor counter in Python, which works by scanning the nginx
access.log
and extracting the time, URL, bytes transferred and HTTP status code of every entry.

It makes a dictionary of unique combinations of URLs and HTTP status codes as keys and the number of times they occur as values.
This dictionary, the latest time and the summed byte count (not site-specific) is saved in a JSON file.
This file is opened on the next start of the program, the log read and then each entry in the log only after the stored time is processed to avoid duplicates.
Currently nothing further is planned with the JSON but maybe a counter on each page will be implemented soon.
The script is called in the
prerotate
block of the logrotate file responsible for nginx.




Back